notifications.ts ➔ getNotificationCount   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
dl 0
loc 8
rs 9.3333
c 0
b 0
f 0
1
import { getGlobalConfiguration, SETTINGS_websiteShowNotificationsCountInTab } from '../configuration/configuration';
2
import * as core from '../utils/aniwatchCore';
3
import * as helper from '../utils/helpers';
4
5
export function init(): void {
6
    getGlobalConfiguration().getProperty(SETTINGS_websiteShowNotificationsCountInTab, value => {
7
        if (value) {
8
            core.runAfterLoad(() => {
9
                updateNotificationsInTitle();
10
            }, ".*");
11
12
            core.runAfterLocationChange(() => {
13
                updateNotificationsInTitle();
14
            }, ".*");
15
        }
16
    });
17
}
18
19
function getNotificationCount(): number {
20
    if (core.isLoggedIn()) {
21
        let menuUserText = document.getElementById('materialize-menu-dropdown').innerText.split('\n')[4];
22
        let notificationCount = parseInt(menuUserText.match(/\d+/)?.[0]) ?? 0;
23
        return notificationCount;
24
    } else {
25
        return 0;
26
    }
27
}
28
29
function updateNotificationsInTitle(): void {
30
    let count = getNotificationCount();
31
32
    if (helper.assigned(count) && count > 0) {
33
        // document.title is updated after the event is triggered, so we delay our title update by a reasonable time
34
        setTimeout(() => {
35
            document.title = `(${count}) ${document.title}`;
36
        }, 100);
37
    }
38
}